// miniprogram/pages/confirmOrder/confirmOrder.js
const {request, getData} = require('../../util/request')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        classId: null,
        product_id: null,
        title: '',
        order_no: '',
        total_price: 0,
        coupon: null,
        couponAmount: 0,
        status: null,
        start_at: '',
        expire_at: '',
        payments: {}
    },
    getCoupon() {
        getData(`/mike/coupon/reference?product_id=${this.data.product_id}`,
            (res) => {
                if (res.error == 0 && res.data.coupon_id) {
                this.setData({
                    coupon: res.data.coupon_id,
                    couponAmount: res.data.discount_amount
                })
                }
            },
            (error) => {
            })
    },
    selectCoupon() {
        wx.navigateTo({
            url: `../coupon/coupon?coupon_id=${this.data.coupon}&product_id=${this.data.product_id}`
        })
    },
    pay() {
        let that = this
        if (!app.globalData.userInfo) {
            // 未绑定 引导绑定手机号
            wx.showToast({
                icon: 'none',
                title: '未绑定账号'
            })
            return false
        }
        let option = {
            coupon_id: this.data.coupon,
            product_id: this.data.product_id,
            openid: this.data.openid
        }
        request('/mike/order/buy', 'POST', option,
            (res) => {
                if (res.error === 0) {
                    let order = res.data.order
                    let payment = JSON.parse(res.data.payments)
                    wx.requestPayment(
                        {
                            appId: payment.appId,
                            nonceStr: payment.nonceStr,
                            package: payment.package,
                            paySign: payment.paySign,
                            signType: payment.signType,
                            timeStamp: payment.timeStamp,
                            success(pays) {
                                wx.showToast({
                                    title: '支付成功',
                                    icon: 'success',
                                    duration: 2000
                                })
                                wx.redirectTo({
                                    url: `../classDetail/classDetail?course_id=${that.data.classId}`
                                })
                            },
                            fail(res) {
                                wx.showModal({
                                    title: '支付失败',
                                    content: '请联系老师，确定异常情况',
                                    showCancel: false,
                                    success(res) {
                                        if (res.confirm) {
                                            // console.log('用户点击确定')
                                        }
                                    }
                                })
                            }
                        },
                    )
                } else {
                    // wx.redirectTo({
                    //     url: `../classDetail/classDetail?course_id=${this.data.classId}`
                    // })
                    wx.showToast({
                        icon: 'none',
                        title: res.message
                    })
                }

            },
            (error) => {
            })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            classId: options.course_id,
            openid: app.globalData.openid
        })
        getData(`/mike/order/confirm/${options.course_id}`,
            (res) => {
                if (res.error === 0) {
                    let order = res.data
                    this.setData({
                        title: order.title,
                        total_price: order.price,
                        product_id: order.product_id,
                        start_at: order.start_at,
                        expire_at: order.expire_at,
                    }, () => {
                        this.getCoupon()
                    })
                }

            },
            (error) => {
            })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
    onShareAppMessage: function (res) {
        return {
            title: '每天半小时，轻松拿本科',
            path: '/pages/index/index',//实验发现，此处的路径应该写全，写到具体的wxml页面。如path:'/page/user/user?id=123'
            success: function(res) {
                // 转发成功
            },
            fail: function(res) {
                // 转发失败
            }
        }
    },

})
